package com.jin.webvideo.parser;

import com.jin.webvideo.js.SourceManager;
import com.jin.webvideo.mapper.ParserMapper;
import com.jin.webvideo.util.sys.ClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.io.IOException;

@Component
public class ParseSourceManager extends SourceManager implements ServletContextAware, SourceManager.SourceLoader {
    @Autowired
    private ParserMapper mapper;
    private static final String header = ";const execFunc = require('./sdk/Must.js');\n";

    private static final String scriptMimeType = "js";

    public ParseSourceManager() {
        super(scriptMimeType);
    }
    @Override
    public void setServletContext(ServletContext servletContext) {
        setSourceLoader(this);
    }

    @Override
    public String load(String name) throws IOException {
        String url = mapper.findUrlByName(name);
        String content = ClientUtil.httpGet(url);
        return header + content;
    }
}
